﻿package mortal.game.view.arena.view
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.control.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.rules.*;
    import mortal.game.utils.*;
    import mortal.game.view.arena.data.*;
    import mortal.game.view.arena.renderer.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.mvc.core.*;

    public class ArenaSignUpPanel extends ArenaPanelBaseNew
    {
        private var _roleName:GTextFiled;
        private var _arenaPoint:GTextFiled;
        private var _currServerRank:GTextFiled;
        private var _regionRank:GTextFiled;
        private var _totalArenaNum:GTextFiled;
        private var _victoryNum:GTextFiled;
        private var _victoryRate:GTextFiled;
        private var _playBtn:GLoadingButton;
        private var _labelImg:GImageBitmap;
        private var _watchingList:GTileList;
        private var _refreshBtn:GButton;
        private var _cancelBtn:GButton;
        private var _defaultTxt:GTextFiled;
        private var _weekPreviewItem:BaseItem;
        private var _monthPreviewItem:BaseItem;
        private var _lastTime:int;

        public function ArenaSignUpPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            this.pushUIToDisposeVec(UIFactory.bg(9 - 2, 58, 282 + 3, 532 + 3, this, ImagesConst.PanelBg));
            this.pushUIToDisposeVec(UIFactory.bg((292 - 1), 58, 609 + 3, 532 + 3, this, ImagesConst.PanelBg));
            this.pushUIToDisposeVec(UIFactory.bg(5, 58 + 2, 790 - 500, 24, this, ImagesConst.RegionTitleBg));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40847), 68 + 35, 61, 83, 20, this));
            this.pushUIToDisposeVec(UIFactory.bg((292 + 1) - 4, 58 + 2, 609 + 8, 24, this, ImagesConst.RegionTitleBg));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40851), 250 + 10 + 74, 61, 43, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40849), 329 + 3 + 74 + 18, 61, 53, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40850), 398 + 3 + 74 + 31, 61, 30, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40848), 515 + 10 + 74 + 27, 61, 35, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40849), 596 + 3 + 74 + 45, 61, 51, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40850), 666 + 3 + 74 + 50, 61, 38, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40852), 731 + 74 + 45, 61, 38, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40853), 22, 93, 76, 20, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40854), 22, 117, 76, 20, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40855), 22, 141, 76, 20, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40856), 22, 165, 76, 20, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40857), 22, 189, 76, 20, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40858), 22, 213, 76, 20, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40859), 134, 213, 76, 20, this, GlobalStyle.textFormatHuang));
            this._roleName = UIFactory.gTextField("", 83, 93, 117, 20, this, GlobalStyle.textFormatFenHong);
            this._arenaPoint = UIFactory.gTextField("", 83, 117, 117, 20, this, GlobalStyle.textFormatLv);
            this._arenaPoint.toolTipData = Language.getString(40860);
            this._currServerRank = UIFactory.gTextField("", 83, 141, 35, 20, this, GlobalStyle.textFormatLv);
            this._regionRank = UIFactory.gTextField("", 83, 165, 40, 20, this, GlobalStyle.textFormatLv);
            this._totalArenaNum = UIFactory.gTextField("", 83, 189, 68, 20, this, GlobalStyle.textFormatLv);
            this._victoryNum = UIFactory.gTextField("", 59 + 25, 213, 68, 20, this, GlobalStyle.textFormatLv);
            this._victoryRate = UIFactory.gTextField("", 171 + 25, 213, 38, 20, this, GlobalStyle.textFormatLv);
            this.pushUIToDisposeVec(UIFactory.bg(7, 58 + 181, 285, 201, this, ImagesConst.MedalBg));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.ArenaSignUpBtnBg, 56 - 41, 253 + 8 - 5, this));
            this._playBtn = UIFactory.gLoadingButton(ResFileConst.ArenaBloodSportBtn, 57 - 44 + 62, 253 + 8, 148, 127, this);
            this._playBtn.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            this._labelImg = UIFactory.gImageBitmap(ImagesConst.ArenaText_BloodSport, 57 - 44 + 38 + 55, 253 + 8 + 29, this);
            this._cancelBtn = UIFactory.gButton(Language.getString(40861), 113 + 6, 394, 63, 20, this);
            this._cancelBtn.visible = false;
            this._cancelBtn.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40862), 45 - 22 + 24, 392 + 9 + 10, 240, 25, this, GlobalStyle.textFormatHuang2.setSize(13)));
            this._weekPreviewItem = UICompomentPool.getUICompoment(BaseItem);
            this._weekPreviewItem.setItemStyle(20, "", 2, 2);
            this._weekPreviewItem.x = 130;
            this._weekPreviewItem.y = 135 + 3;
            this._weekPreviewItem.isShowPrescritime = false;
            this.addChild(this._weekPreviewItem);
            this._monthPreviewItem = UICompomentPool.getUICompoment(BaseItem);
            this._monthPreviewItem.setItemStyle(20, "", 2, 2);
            this._monthPreviewItem.x = 130;
            this._monthPreviewItem.y = 135 + 26 + 4;
            this._monthPreviewItem.isShowPrescritime = false;
            this.addChild(this._monthPreviewItem);
            this.updatePreviewItem();
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40863), 158, 141, 124, 20, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40863), 158, 165, 124, 20, this));
            this._watchingList = UIFactory.tileList(227 + 76, 81, 564 + 34, 350, this);
            this._watchingList.columnWidth = 548 + 34;
            this._watchingList.rowHeight = 31 + 4;
            this._watchingList.horizontalGap = 0;
            this._watchingList.verticalGap = 0;
            this._watchingList.setStyle("skin", new Bitmap());
            this._watchingList.setStyle("cellRenderer", ArenaWatchingListCellRenderer);
            this._refreshBtn = UIFactory.gButton(Language.getString(40864), 473 + 69 + 8, 398 + 2 + 8, 63, 20, this);
            this._refreshBtn.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            var _loc_1:* = GlobalStyle.textFormatHui;
            _loc_1.size = 22;
            _loc_1.bold = true;
            _loc_1.align = AlignMode.CENTER;
            this._defaultTxt = UIFactory.gTextField("", 293, 112 + 100 + 7, 550, 33, this, _loc_1);
            super.createDisposedChildrenImpl();
            this.initBtnState();
            this.initDefaultTxt();
            this.reqAllInfo();
            this.addListeners();
            return;
        }// end function

        private function reqAllInfo() : void
        {
            GameProxy.copy.getMyArenaInfo(ECopyType._ECopyTypeArena);
            GameProxy.copy.getArenaBattles(ECopyType._ECopyTypeArena);
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.ArenaGetRoleArenaInfo, this.onGetMyArenaInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaGetWatchingList, this.onGetWatchingListHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaMatchSucc, this.onMatchSuccHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaLeaveArena, this.onLeaveArenaHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaGoldUpdate, this.onArenaGoldUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ArenaEnterArena, this.onEnterArenaHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaSignUp, this.onSignUpOrCancelHandler);
            Dispatcher.addEventListener(EventName.ArenaBegin, this.onArenaBegin);
            Dispatcher.addEventListener(EventName.ArenaEnd, this.onArenaEnd);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.ArenaGetRoleArenaInfo, this.onGetMyArenaInfoHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ArenaGetWatchingList, this.onGetWatchingListHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ArenaMatchSucc, this.onMatchSuccHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ArenaLeaveArena, this.onLeaveArenaHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ArenaGoldUpdate, this.onArenaGoldUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.ArenaEnterArena, this.onEnterArenaHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ArenaSignUp, this.onSignUpOrCancelHandler);
            Dispatcher.removeEventListener(EventName.ArenaBegin, this.onArenaBegin);
            Dispatcher.removeEventListener(EventName.ArenaEnd, this.onArenaEnd);
            return;
        }// end function

        private function onBtnClickHandler(event:MouseEvent) : void
        {
            var _loc_3:GroupCopyLeaveOperData = null;
            if (Cache.instance.group.isInCopyGroup)
            {
                _loc_3 = new GroupCopyLeaveOperData(GroupCopyLeaveOperData.RegisterArena);
                _loc_3.tcopy = CopyConfig.instance.getCopyInfoByCode(Cache.instance.group.sgroup.copyCode);
                Dispatcher.dispatchEvent(new DataEvent(EventName.GroupLeave, _loc_3));
                return;
            }
            if (CopyUtil.isInAllEscortTask)
            {
                MsgManager.showRollTipsMsg(Language.getString(40865));
                return;
            }
            if (this._lastTime == 0)
            {
                this._lastTime = getTimer();
            }
            else
            {
                if (getTimer() - this._lastTime < 1000)
                {
                    MsgManager.showRollTipsMsg(Language.getString(40866));
                    return;
                }
                this._lastTime = getTimer();
            }
            var _loc_2:* = new EArenaMode(EArenaMode._EArenaOneVsMulti);
            if (event.target == this._playBtn)
            {
                GameProxy.copy.signUpArena(ECopyType._ECopyTypeArena, false, _loc_2);
            }
            if (event.target == this._cancelBtn)
            {
                GameProxy.copy.signUpArena(ECopyType._ECopyTypeArena, true, _loc_2);
            }
            if (event.target == this._refreshBtn)
            {
                GameProxy.copy.getArenaBattles(ECopyType._ECopyTypeArena);
            }
            return;
        }// end function

        private function onSignUpOrCancelHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SArenaSignUpState;
            if (_loc_2.isCancel)
            {
                this.isCancelAvailable = false;
                this.isSignUpAvailable = true;
            }
            else
            {
                this.isCancelAvailable = true;
                this.isSignUpAvailable = false;
            }
            return;
        }// end function

        private function initBtnState() : void
        {
            if (GameController.arena.isMatching || GameController.arena.isMatchSucc || GameController.arena.isInArena)
            {
                this.isSignUpAvailable = false;
            }
            else
            {
                this.isSignUpAvailable = true;
            }
            if (GameController.arena.isMatching)
            {
                this.isCancelAvailable = true;
            }
            else
            {
                this.isCancelAvailable = false;
            }
            return;
        }// end function

        public function restBtnState() : void
        {
            this.isSignUpAvailable = true;
            this.isCancelAvailable = false;
            return;
        }// end function

        private function initDefaultTxt() : void
        {
            if (!GameController.arena.isArenaBegin && !GameController.arena.isArenaEnd)
            {
                this._defaultTxt.text = Language.getString(40867);
            }
            else if (GameController.arena.isArenaClose || GameController.arena.isArenaBegin && GameController.arena.isArenaEnd)
            {
            }
            return;
        }// end function

        private function onGetMyArenaInfoHandler(param1:SMyArenaInfo) : void
        {
            Cache.instance.arena.myArenaInfo = param1;
            this._roleName.text = param1.name;
            this._arenaPoint.htmlText = HTMLUtil.addLink(param1.score + "", GlobalStyle.colorLv);
            this._currServerRank.text = param1.rank == 0 ? (Language.getString(40869)) : (param1.rank + "");
            this._regionRank.text = param1.crossRank == 0 ? (Language.getString(40869)) : (param1.crossRank + "");
            this._totalArenaNum.text = param1.winNum + param1.loseNum + Language.getString(40870);
            this._victoryNum.text = param1.winNum + Language.getString(40870);
            var _loc_2:* = param1.winNum;
            var _loc_3:* = param1.winNum + param1.loseNum;
            var _loc_4:* = int(_loc_2 * 100 / _loc_3);
            this._victoryRate.text = _loc_4 + "%";
            var _loc_5:* = ArenaController.ArenaLevel;
            var _loc_6:* = ArenaRewardConfig.instance.getInfosByTypeAndLevel(EArenaRewardType._EArenaRewardTypeWeek, _loc_5, ECopyType._ECopyTypeArena);
            var _loc_7:* = ArenaRewardConfig.instance.getInfosByTypeAndLevel(EArenaRewardType._EArenaRewardTypeMouth, _loc_5, ECopyType._ECopyTypeArena);
            this._weekPreviewItem.itemCode = (_loc_6[0] as TArenaReward).itemCode;
            this._monthPreviewItem.itemCode = (_loc_7[0] as TArenaReward).itemCode;
            return;
        }// end function

        private function onGetWatchingListHandler(param1:SArenaCrossBattles) : void
        {
            var _loc_5:SArenaCrossBattle = null;
            var _loc_6:SArenaBasePlayer = null;
            var _loc_7:SArenaBasePlayer = null;
            var _loc_8:ArenaWatchingListData = null;
            var _loc_9:int = 0;
            var _loc_2:* = param1.battles;
            var _loc_3:Array = [];
            if (_loc_2.length != 0)
            {
                if (_loc_2.length > 9)
                {
                    _loc_2.splice(9);
                }
                for each (_loc_5 in _loc_2)
                {
                    
                    _loc_6 = _loc_5.groupOne.playerInfos[0] as SArenaBasePlayer;
                    _loc_7 = _loc_5.groupTwo.playerInfos[0] as SArenaBasePlayer;
                    if (_loc_6 && _loc_7)
                    {
                        _loc_8 = new ArenaWatchingListData();
                        _loc_8.arenaId = _loc_5.arenaId;
                        _loc_8.leftRoleName = _loc_6.name;
                        _loc_8.leftCombat = _loc_6.combat;
                        _loc_8.leftPoint = _loc_5.groupOne.score;
                        _loc_8.rightRoleName = _loc_7.name;
                        _loc_8.rightCombat = _loc_7.combat;
                        _loc_8.rightPoint = _loc_5.groupTwo.score;
                        _loc_3.push(_loc_8);
                    }
                }
            }
            else if (_loc_2.length == 0 && this._defaultTxt.text != "")
            {
                _loc_9 = 0;
                while (_loc_9 < 9)
                {
                    
                    _loc_3.push({});
                    _loc_9++;
                }
            }
            var _loc_4:* = new DataProvider();
            _loc_4.addItems(_loc_3);
            this._watchingList.dataProvider = _loc_4;
            this._watchingList.drawNow();
            return;
        }// end function

        private function onMatchSuccHandler(param1:SArenaMatch) : void
        {
            this._cancelBtn.visible = false;
            return;
        }// end function

        private function onLeaveArenaHandler(param1:Object) : void
        {
            this.isSignUpAvailable = true;
            this.isCancelAvailable = false;
            return;
        }// end function

        private function set isSignUpAvailable(param1:Boolean) : void
        {
            if (param1)
            {
                this._playBtn.filters = [];
                this._playBtn.mouseEnabled = true;
                this._labelImg.imgUrl = ImagesConst.ArenaText_BloodSport;
                this._labelImg.filters = [];
                if (GameController.arena.isInArena)
                {
                    this._labelImg.imgUrl = ImagesConst.ArenaText_StartSport;
                }
            }
            else
            {
                this._playBtn.filters = [FilterConst.colorFilter2];
                this._playBtn.mouseEnabled = false;
                this._labelImg.imgUrl = ImagesConst.ArenaText_WaitMatch;
                this._labelImg.filters = [FilterConst.colorFilter2];
            }
            return;
        }// end function

        private function onArenaGoldUpdate(param1:SMoney) : void
        {
            return;
        }// end function

        private function onEnterArenaHandler(param1:Object) : void
        {
            if (GameController.arena.isInArena && !GameController.arena.isWatching)
            {
                this.isSignUpAvailable = false;
                this.isCancelAvailable = false;
            }
            return;
        }// end function

        private function onArenaBegin(event:DataEvent) : void
        {
            this._defaultTxt.text = "";
            var _loc_2:* = new DataProvider();
            _loc_2.addItems([]);
            this._watchingList.dataProvider = _loc_2;
            this._watchingList.drawNow();
            return;
        }// end function

        private function onArenaEnd(event:DataEvent) : void
        {
            this.isSignUpAvailable = true;
            this.isCancelAvailable = false;
            return;
        }// end function

        private function onLinkClickHandler(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.NPC_OpenNpcShop, ShopType.Arena));
            return;
        }// end function

        private function set isCancelAvailable(param1:Boolean) : void
        {
            this._cancelBtn.visible = param1;
            return;
        }// end function

        private function updatePreviewItem() : void
        {
            var _loc_1:* = ArenaController.ArenaLevel;
            var _loc_2:* = ArenaRewardConfig.instance.getInfosByTypeAndLevel(EArenaRewardType._EArenaRewardTypeWeek, _loc_1, ECopyType._ECopyTypeArena);
            if (_loc_2 && _loc_2.length > 0)
            {
                this._weekPreviewItem.itemCode = (_loc_2[0] as TArenaReward).itemCode;
            }
            var _loc_3:* = ArenaRewardConfig.instance.getInfosByTypeAndLevel(EArenaRewardType._EArenaRewardTypeMouth, _loc_1, ECopyType._ECopyTypeArena);
            if (_loc_3 && _loc_3.length > 0)
            {
                this._monthPreviewItem.itemCode = (_loc_3[0] as TArenaReward).itemCode;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._roleName.dispose(param1);
            this._arenaPoint.dispose(param1);
            this._currServerRank.dispose(param1);
            this._regionRank.dispose(param1);
            this._totalArenaNum.dispose(param1);
            this._victoryNum.dispose(param1);
            this._victoryRate.dispose(param1);
            this._playBtn.dispose(param1);
            this._watchingList.dispose(param1);
            this._refreshBtn.dispose(param1);
            this._cancelBtn.dispose(param1);
            this._defaultTxt.dispose();
            this._labelImg.dispose();
            this._weekPreviewItem.dispose();
            this._monthPreviewItem.dispose();
            this._roleName = null;
            this._arenaPoint = null;
            this._currServerRank = null;
            this._regionRank = null;
            this._totalArenaNum = null;
            this._victoryNum = null;
            this._victoryRate = null;
            this._playBtn = null;
            this._watchingList = null;
            this._refreshBtn = null;
            this._cancelBtn = null;
            this._defaultTxt = null;
            this._labelImg = null;
            this._weekPreviewItem = null;
            this._monthPreviewItem = null;
            this.removeListeners();
            return;
        }// end function

    }
}
